﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeHeng_Mes.Mapper
{
    public static class ErrorMappingTable
    {
        /// <summary>
        /// 故障类型名称映射表
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetTypeName(string type)
        {
            Dictionary<string,string> dic = new Dictionary<string, string>()
            {
                {"Independent1","上游PLC关联故障" },
                {"Independent2","下游PLC关联故障" },

                {"Robot","机器人状态" },
                {"Jig_A","A面夹具故障" },
                {"Jig_B","B面夹具故障" },
                {"Jig_A_Sen","A面夹具传感器故障" },
                {"Jig_B_Sen","B面夹具传感器故障" },
                {"Motor","电机故障" },
                {"SG_ST","安全门故障" },
                {"Raster_st","光栅故障" },
                {"Estop","急停" },
                {"State","通讯故障" },
            };

            if(dic.TryGetValue(type, out string typeName))
            {
                return typeName;
            }
            return null;
        }

        /// <summary>
        /// 反向获取Key
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetTypeKey(string value)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>()
            {
                {"Independent1","上游PLC关联故障" },
                {"Independent2","下游PLC关联故障" },

                {"Robot","机器人状态" },
                {"Jig_A","A面夹具故障" },
                {"Jig_B","B面夹具故障" },
                {"Jig_A_Sen","A面夹具传感器故障" },
                {"Jig_B_Sen","B面夹具传感器故障" },
                {"Motor","电机故障" },
                {"SG_ST","安全门故障" },
                {"Raster_st","光栅故障" },
                {"Estop","急停" },
                {"State","通讯故障" },
            };

            foreach (var item in dic)
            {
                if(item.Value.Equals(value))
                    return item.Key;
            }
            return null;
        }

        /// <summary>
        /// 获取详细信息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="key"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        public static string GetMessage(string plcName, string errorType, int index)
        {
            Dictionary<string, Dictionary<string, Dictionary<int, string>>> dic = new Dictionary<string, Dictionary<string, Dictionary<int, string>>>();

            dic["P4"] = new Dictionary<string, Dictionary<int, string>>()
            {
                { "Jig_A",new Dictionary<int, string>()
                {

                    { 0,"STA10_JIG报错"},
                } },
                { "Jig_B",new Dictionary<int, string>()
                {

                    { 9,"STA20_JIG报错"},
                } },
                { "Jig_A_Sen",new Dictionary<int, string>()
                {

                    { 0,"STA10_JIG_SEN报错"},
                } },
                { "Jig_B_Sen",new Dictionary<int, string>()
                {
                    { 0,"STA20_JIG_SEN报错"},
                } },
                { "Motor",new Dictionary<int, string>()
                {
                } },
                { "SG_ST",new Dictionary<int, string>()
                {
                    { 0,"安全门打开"},
                    { 1,"安全门故障"},
                } },
                { "Raster_st",new Dictionary<int, string>()
                {
                    { 0,"上件口光栅被闯入"},
                } },
                { "Estop",new Dictionary<int, string>()
                {
                    { 0,"HMI急停"},
                    { 1,"OB盒急停"},
                    { 2,"安全门急停"},
                    { 3,"R1机器人急停"},
                    { 4,"R2机器人急停"},
                } },
                { "State",new Dictionary<int, string>()
                {
                    { 0,"CPU通讯故障"},
                    { 11,"HMI1通讯故障"},
                    { 25,"MCP-ET200通讯故障"},
                    { 26,"STA10-ET200通讯故障"},
                    { 27,"STA20-ET200通讯故障"},
                    { 100,"STA10-PM通讯故障"},
                    { 101,"STA20-PM通讯故障"},
                    { 121,"R1通讯故障"},
                    { 122,"R2通讯故障"},
                } },
            };

            if(dic.TryGetValue(plcName,out Dictionary<string,Dictionary<int, string>> value))
            {
                if(value.TryGetValue(errorType, out Dictionary<int, string> errorValue))
                {
                    if (errorValue.TryGetValue(index, out string errorMessage))
                        return errorMessage;
                }
            }

            return null;
        }
    }
}
